package com.zmn.oms.admin.controller.conf.attachment;

import com.alibaba.fastjson.JSON;
import com.zmn.common.dto2.data.LayuiTableDO;
import com.zmn.oms.admin.controller.OmsBaseController;
import com.zmn.oms.business.interfaces.conf.attachment.ConfOrderAttachmentCategoryBService;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.entity.conf.attachment.ConfOrderAttachmentCategory;
import com.zmn.oms.model.entity.conf.attachment.ConfOrderAttachmentCategoryQuery;
import com.zmn.oms.model.vo.conf.attachment.ConfOrderAttachmentCategoryVO;
import com.zmn.oms.services.interfaces.conf.attachment.ConfOrderAttachmentCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;


/**
 * 类描述：照片类型配置
 *
 * @author ligang
 * @date 2021/09/25 11:03
 */
@Controller
@RequestMapping("/conf/attachment/category")
@Slf4j
class ConfOrderAttachmentCategoryController extends OmsBaseController {

    @Autowired
    private ConfOrderAttachmentCategoryService confOrderAttachmentCategoryService;
    @Autowired
    private ConfOrderAttachmentCategoryBService confOrderAttachmentCategoryBService;


    @RequestMapping(value = "/list")
    public ModelAndView list(ModelAndView mv) {
        mv.setViewName("conf/attachment/category/list");
        return mv;
    }

    /**
     * 查看/编辑
     *
     * @param mv
     * @param categId
     * @return
     */
    @RequestMapping(value = "/addEdit")
    public ModelAndView detail(ModelAndView mv, Integer categId) {
        mv.setViewName("conf/attachment/category/detail");
        ConfOrderAttachmentCategory category;
        if (Objects.nonNull(categId)) {
            category = confOrderAttachmentCategoryService.findCategoryById(categId);
        } else {
            category = ConfOrderAttachmentCategory.builder().build();
        }
        mv.addObject("data", category);
        return mv;
    }

    /**
     * 查询列表 ajax
     */
    @RequestMapping(value = "/ajax/list")
    @ResponseBody
    public ResultDTO ajaxList(ConfOrderAttachmentCategoryQuery query) {
        log.info("ConfOrderAttachmentCategoryQuery[{}]", JSON.toJSONString(query));
        List<ConfOrderAttachmentCategoryVO> confList = confOrderAttachmentCategoryBService.listPageByQuery(query);
        LayuiTableDO<ConfOrderAttachmentCategoryVO> data = new LayuiTableDO();
        data.setCount(query.getTotalCount());
        data.setData(confList);
        return ResultDTO.success(data);
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public ResultDTO save(@RequestBody ConfOrderAttachmentCategory conf) {
        try {
            if (Objects.nonNull(conf.getCategId())) {
                conf.setUpdater(super.getCurrentStaff().getRealName());
                confOrderAttachmentCategoryBService.modifyCategoryById(conf);
                return ResultDTO.success();
            } else {
                conf.setCreater(super.getCurrentStaff().getRealName());
                Integer id = confOrderAttachmentCategoryBService.addCategory(conf);
                return ResultDTO.success(id);
            }
        } catch (OmsBaseException e) {
            e.printStackTrace();
            return ResultDTO.fail(e.getMessage());
        }
    }

    /**
     * 改状态
     */
    @RequestMapping(value = "/modifyStatus")
    @ResponseBody
    public ResultDTO modifyStatus(ConfOrderAttachmentCategory conf) throws OmsBaseException {
        log.info("confCheckout[{}]", conf);
        conf.setUpdater(super.getCurrentStaff().getRealName());
        confOrderAttachmentCategoryBService.modifyCategoryStatusById(conf);
        return ResultDTO.success();
    }

    /**
     * 批量修改状态
     */
    @RequestMapping(value = "/batch/modifyStatus")
    @ResponseBody
    public ResultDTO modifyStatus(ConfOrderAttachmentCategory conf, String ids) {
        log.info("confCheckout[{}]", conf);
        conf.setUpdater(super.getCurrentStaff().getRealName());
        List<String> categIdList = Arrays.asList(ids.split(","));
        confOrderAttachmentCategoryBService.batchModifyCategoryStatus(conf, categIdList);
        return ResultDTO.success();
    }

    /**
     * 改sort
     */
    @RequestMapping(value = "/modifySort")
    @ResponseBody
    public ResultDTO modifySort(ConfOrderAttachmentCategory conf) {
        log.info("confCheckout[{}]", conf);
        conf.setUpdater(super.getCurrentStaff().getRealName());
        confOrderAttachmentCategoryBService.modifyCategorySortById(conf);
        return ResultDTO.success();
    }

    /**
     * 类型名称重复性校验
     */
    @RequestMapping(value = "/category/name/check")
    @ResponseBody
    public ResultDTO typeNameRepeatabilityCheck(ConfOrderAttachmentCategoryQuery conf) {
        int count = confOrderAttachmentCategoryService.countByQuery(conf);
        return ResultDTO.success(count);
    }
}
